﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using AccesoDatos;

namespace RMA.Pantallas
{
    public partial class frmPrincipal : Form
    {
        public List<clsUsuarios> ListaPermisos = new List<clsUsuarios>();
        private string _strUser = "";
        private string _strPasswd = "";
        private int _App;
        private int _Suc;
        DataTable dtUser = null;
        [DllImport("user32.dll")]
        static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, uint dwFlags);

        public class WinApi
        {
            public static int GWL_EXSTYLE = -20;
            public static int WS_EX_LAYERED = 0x80000;
            public static int LWA_ALPHA = 0x2;
            [DllImport("user32")]
            public static extern int SetLayeredWindowAttributes(IntPtr hWnd, byte crey, byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int GetLayeredWindowAttributes(IntPtr hWnd, byte crey, out byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
            [DllImport("user32")]
            public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        }
        void mkwndtrans(IntPtr hWnd, int num)
        {
            int oldWinStyle = WinApi.GetWindowLong(hWnd, WinApi.GWL_EXSTYLE);
            WinApi.SetWindowLong(hWnd, WinApi.GWL_EXSTYLE,
            num < 255 ? oldWinStyle | WinApi.WS_EX_LAYERED :
            oldWinStyle & WinApi.WS_EX_LAYERED);
            WinApi.SetLayeredWindowAttributes(hWnd, 0, (byte)num, WinApi.LWA_ALPHA);
        }
        enum AnimateWindowFlags : uint
        {
            AW_HOR_POSITIVE = 0x00000007,
            AW_HOR_NEGATIVE = 0x00000002,
            AW_VER_POSITIVE = 0x00000004,
            AW_VER_NEGATIVE = 0x00000008,
            AW_CENTER = 0x00000010,
            AW_HIDE = 0x00010000,
            AW_ACTIVATE = 0x00020000,
            AW_SLIDE = 0x00040000,
            AW_BLEND = 0x00080000
        }
        public static int IdUsr;
        public static string SucursalId;
        public frmPrincipal()
        {
            InitializeComponent();
        }

        public frmPrincipal(List<clsUsuarios> lstPermisos, string strUsuario, string strPasswd, DataTable _dtUser ,int app,int suc)
        {
            ListaPermisos = lstPermisos;
            _strUser = strUsuario;
            _strPasswd = strPasswd;
            _App = app;
            _Suc = suc;
            SucursalId = suc.ToString();
            dtUser = _dtUser;
            foreach (DataRow r in dtUser.Rows)
            {
                IdUsr = int.Parse(r[1].ToString());
            }
            InitializeComponent();
        }

        private void proveedoresToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCatProveedores frmP = new frmCatProveedores();
            AnimateWindow(frmP.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmP.MdiParent = this;
            frmP.Show();
        }

        private void usuariosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCatUsuarios frmU = new frmCatUsuarios();
            AnimateWindow(frmU.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmU.MdiParent = this;
            frmU.Show();
        }

        private void mantenimientoATicketsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmMantenimientoTicket a = new frmMantenimientoTicket(dtUser);
            AnimateWindow(a.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            a.MdiParent = this;
            a.Show();            
        }

        private void centrosServicioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCatCentrosServ frmCS = new frmCatCentrosServ();
            AnimateWindow(frmCS.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmCS.MdiParent = this;
            frmCS.Show();
        }

        private void tramiteRMAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmTramitar frmT = new frmTramitar();
            AnimateWindow(frmT.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmT.MdiParent = this;
            frmT.Show();
        }

        private void recepcionDeMercanciaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_RecepcionMercancia a = new frm_RecepcionMercancia();
            AnimateWindow(a.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            a.MdiParent = this;
            a.Show();
        }

        private void autorizacionDeRMAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_AutorizacionTicket a = new frm_AutorizacionTicket(dtUser);
            AnimateWindow(a.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            a.MdiParent = this;
            a.Show();
        }

        public void AplicaPermisos()
        {
            int a = 0;
            ListaPermisos.ForEach(delegate(clsUsuarios objUser)
            {
                a++;
                if (objUser.App_Id == 7) 
                {
                    sucursalToolStripMenuItem.Visible = false;
                }
                if (objUser.Mod_Id == 1)
                {
                    catalogosToolStripMenuItem.Visible = true;
                }
                else
                {
                    if (objUser.Mod_Id == 2)
                    {
                        proveedoresToolStripMenuItem.Visible = true;
                    }
                    else
                    {
                        if (objUser.Mod_Id == 3)
                        {
                            usuariosToolStripMenuItem.Visible = true;
                        }
                        else
                        {
                            if (objUser.Mod_Id == 4)
                            {
                                centrosServicioToolStripMenuItem.Visible = true;
                            }
                            else
                            {
                                if (objUser.Mod_Id == 5)
                                {
                                    operacionesToolStripMenuItem.Visible = true;
                                }
                                else
                                {
                                    if (objUser.Mod_Id == 6)
                                    {
                                        mantenimientoATicketsToolStripMenuItem.Visible = true;
                                    }
                                    else
                                    {
                                        if (objUser.Mod_Id == 7)
                                        {
                                            tramiteRMAToolStripMenuItem.Visible = true;
                                        }
                                        else
                                        {
                                            if (objUser.Mod_Id == 8)
                                            {
                                                pagosToolStripMenuItem.Visible = true;
                                            }
                                            else
                                            {
                                                if (objUser.Mod_Id == 9)
                                                {
                                                    altaPagosToolStripMenuItem.Visible = true;
                                                }
                                                else
                                                {
                                                    if (objUser.Mod_Id == 10)
                                                    {
                                                        autorizacionDeRMAToolStripMenuItem.Visible = true;
                                                    }
                                                    else
                                                    {
                                                        if (objUser.Mod_Id == 11)
                                                        {
                                                            recepcionDeMercanciaToolStripMenuItem.Visible = true;
                                                        }
                                                        else
                                                        {
                                                            if (objUser.Mod_Id == 12)
                                                            {
                                                                totalesPorSucursalToolStripMenuItem.Visible = true;
                                                            }
                                                            else
                                                            {
                                                                if (objUser.Mod_Id == 13)
                                                                {
                                                                    reporteGeneralToolStripMenuItem.Visible = true;
                                                                }
                                                                else
                                                                {
                                                                    if (objUser.Mod_Id == 14)
                                                                    {
                                                                        antiguToolStripMenuItem.Visible = true;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }



            });
        }

        public void AplicaPermisosSucursales()
        {
            int a = 0;
            ListaPermisos.ForEach(delegate(clsUsuarios objUser)
            {
                a++;
                if (objUser.App_Id == 12)
                {
                    catalogosToolStripMenuItem.Visible = false;
                    operacionesToolStripMenuItem.Visible = false;
                    reportesToolStripMenuItem.Visible = false;
                }

                if (objUser.Mod_Id == 1)
                {
                    consultaToolStripMenuItem.Visible = true;
                }

                if (objUser.Mod_Id == 2)
                {
                    crearOToolStripMenuItem.Visible = true;
                }
                if (objUser.Mod_Id == 3)
                {
                    embarquesToolStripMenuItem.Visible = true;
                }
                if (objUser.Mod_Id == 4)
                {
                    AlmacenToolStripMenuItem.Visible = true;
                }
                if (objUser.Mod_Id == 5)
                {
                    reportesToolStripMenuItem1.Visible = true;
                }
                
            });
        }

        private void pagosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAplicarPagos frmAP = new frmAplicarPagos(ListaPermisos);
            AnimateWindow(frmAP.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmAP.MdiParent = this;
            frmAP.Show();
        }

        private void altaPagosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmAltaPagos frmA = new frmAltaPagos();
            frmConsultaAltaPagos frmCP = new frmConsultaAltaPagos();
            AnimateWindow(frmCP.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmCP.Show();
        }
        
        private void frmPrincipal_Load(object sender, EventArgs e)
        {
            
                if (_App == 7) 
                {
                    AplicaPermisos();
                }
                if (_App == 12) 
                {
                    AplicaPermisosSucursales();
                }
                
            lblUsr.Text = _strUser;
            lblPssw.Text = _strPasswd;
        }

        private void frmPrincipal_FormClosed_1(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void totalesPorSucursalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmPrmReporteTotales frmT = new frmPrmReporteTotales();
            AnimateWindow(frmT.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmT.Show();
        }

        private void reporteGeneralToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmReporteGeneral frmG = new frmReporteGeneral();
            AnimateWindow(frmG.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmG.Show();
        }

        private void antiguToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmReporteAntiguedad frmA = new frmReporteAntiguedad();
            AnimateWindow(frmA.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmA.Show();
        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void reporteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmReporteRMAGeneral frmRMA = new frmReporteRMAGeneral();
            AnimateWindow(frmRMA.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmRMA.ShowDialog();
        }

        private void estatusToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmEstatus EST = new frmEstatus();
            AnimateWindow(EST.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            EST.ShowDialog();
        }

        private void sucursalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void catalogosToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void operacionesToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void reportesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void crearOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCreaOrdenServicio obCOS = new frmCreaOrdenServicio();
            obCOS.ShowDialog();
        }

        private void embarquesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmEmbarque objEm = new frmEmbarque(_Suc, _strUser);
            AnimateWindow(objEm.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            objEm.ShowDialog();
        }

        private void reportesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmExcel exc = new frmExcel(_strUser,_Suc);
            AnimateWindow(exc.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            exc.ShowDialog();
        }

        private void salirToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void consultaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSucursalConsulta suc = new frmSucursalConsulta(_Suc,_strUser);
            AnimateWindow(suc.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            suc.ShowDialog();
        }

        private void AlmacenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAlmacen almacen = new frmAlmacen();
            AnimateWindow(almacen.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            almacen.ShowDialog();
        }

        private void generarNuevoCodigoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCodigoAut frmC = new frmCodigoAut();
            AnimateWindow(frmC.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmC.Show();
        }

        private void ordenDeServicioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCreaOrdenServicio frmCO = new frmCreaOrdenServicio();
            AnimateWindow(frmCO.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmCO.Show();
        }

        private void ordenDeServicioInternaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCreaOrdenInterna frmOI = new frmCreaOrdenInterna();
            AnimateWindow(frmOI.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmOI.Show();
        }
    }
}
